DataControl Class

Allows the user to browse among records in a database table by clicking buttons. It consists of First Record, Previous Record, Next Record, and Last Record navigation buttons and a label that displays the Caption property.

Events

Delete

MovePrevious

Insert

Reposition

MoveFirst

Update

MoveLast

Validate

MoveNext

 

Properties

None

Methods

Delete

MoveNext

RecordSet

FieldCount

MovePrevious

Row

Insert

MoveTo

RowCount

MoveFirst

NewRecord

RunQuery

MoveLast

RecordCount

Update


More information available in parent classes: RectControl:Control:Object

Built-in events fire when the user clicks any button. Methods allow you to navigate among rows programmatically.

A DataControl is designed to display data using CheckBoxes, EditFields, ListBoxes, StaticTexts, PopupMenus, and ComboBoxes. You can also create custom classes that are based on any of these classes and use them in conjunction with a DataControl.

Each of these controls (or their subclasses) has two properties that are specific to the DataControl: DataSource and DataField. DataSource accepts a DataControl object and DataField accepts the name of a field in the table to which the DataControl is linked. You set up a database interface by linking a DataControl to a table in a database and then linking each field whose values you wish to display to a CheckBox, EditField, ListBox, StaticText, PopupMenu or ComboBox using the DataField and DataSource properties.


Properties

PropertyTypeDescription
Caption String The text shown between the two sets of navigation buttons.
Commit Boolean If True, automatically commits inserts, updates, and deletions to the database.
Database Database The database to which the control is linked.
Encoding TextEncoding When the DataControl retrieves a string value, it will set the Encoding of the string and when you update or insert a string value using the DataControl. REALbasic will convert that encoding to this encoding. The default Encoding is UTF8.
ReadOnly Boolean Set to True to prevent the user from adding, modifying, or deleting records. When True, the record navigation controls are disabled.
RecordLocked Boolean A value of True means that the record is locked, preventing changes to the record; False indicates that it is unlocked.
RecordSet RecordSet The RecordSet managed by the DataControl.
SQLQuery String SQL query used to obtain the default RecordSet.
TableName String Name of the table in Database whose records will be displayed by the controls linked to the DataControl.


Notes

You use a DataControl by creating a window that uses Checkboxes, EditFields, ListBoxes, PopupMenus, ComboBoxes, or StaticText controls to display and edit data. Each of these controls has two properties that are meaningful only when used in conjunction with a DataControl, DataField and DataSource.

You can use Object Binding to bind objects such as a PushButton to a DataControl. Supported actions are: Add, Insert, Update, and Delete Record when the PushButton is pushed. To create an object bind, select the control and the DataControl and then choose Project . Add . Binding (If you have added the Add Binding button to the Window Editor toolbar, you can click that button to display the dialog). A dialog box that lists all possible bindings between the selected control and the DataControl will be listed. Choose the desired type of binding.

To remove a binding, choose View . List Bindings. A dialog that lists all the current bindings in the window appears. Highlight the binding you wish to remove and then click the Delete button. (If you have added the List Bindings button to the Window Editor toolbar, you can click that button to display the dialog.)

When you build an interface using a DataControl, it makes it easy to modify and save records. When the user saves changes, it automatically updates the values in the database back end with the values in the controls bound to the DataControl. If you need to update other values, you need to do it conventionally using the RecordSet method of the DataControl.


Example

The following example database displays fields in EditFields and uses a DataControl for record navigation.

The DataControl's properties in the Behavior group in its Properties Window specify that it will manage the records in the Movies table in the RealDB database.

The SQLQuery property is the SQL query that will run when the form is opened. This query finds all the records in the Movies table. This means that the DataControl's navigation buttons can be used to browse through all the records right after the form opens.

Each EditField is linked to the DataControl in its Properties Window by setting the DataSource and DataField properties. When you set the DataSource, a pop-up menu of fields from the table specified by Database becomes available for the DataField property. For example, the EditField for the movie title is bound to the column "Title" in the table to which the DataControl is linked.

Each field on the form is bound to a field in the Movies table in this manner.

The Add button is linked to the DataControl using object binding. To establish the bind, select both the PushButton and the DataControl and choose Project . Add . Binding to display the New Binding dialog box. Select "New Record when addButton pushed". (If you have added the Add Binding button to the Window Editor toolbar, you can click that instead of choosing the menu command.)

Use the same process to bind the Delete button to the DataControl and use "Delete Record when DeleteButton pushed." Bind the Insert button to the "Insert record when InsertButton pushed" and bind the Update button to the "Update record when UpdateButton pushed.

To add a new record, click the Add button, enter the information into the fields, and click Insert. To modify a record, navigate to it using the buttons in the DataControl, edit the values, and click Update. To delete a record, navigate to it using the buttons in the DataControl and click Delete.


See Also

Database, DatabaseField, DatabaseRecord, RecordSet classes.